package com.etone.smartAudit.utils;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: sovy
 * @date: 2020/12/18
 * @description:
 */
public class IpUtil {

    /**
     * 提取json字符串中的ip
     *
     * @param str
     * @return
     */
    public static List<String> getJsonIps(String str) {
        List<String> ips = new ArrayList<>();
        String regex = "^(.*)\\\"(((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?))\\\"(.*)$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            String ip = matcher.group(2);
            if (!StringUtils.isEmpty(ip)) {
                ips.add(ip);
            }
        }
        return ips;
    }
}
